$(function() {
	
	// on clicking change photo show loading image
	$('#profilePhoto').live('change', function(){
		$('#modalWrap').fadeOut(300);
		$('#fileUploadForm').submit();
		$('#dpOverlay').show();
		//hide popup
		$('#chng_prfl_pht_link').removeClass("hide");
		$('#chng_prfl_pht_popup').addClass("hide");

	});
	
	document.getElementById("fileUploadForm").onsubmit=function()	{
		document.getElementById("uploadTarget").onload = uploadDone(); //This function should be called when the iframe has compleated loading
		// That will happen when the file is completely uploaded and the server has returned the data we need.
	}
                
});
        
        
function init(){
	document.getElementById("fileUploadForm").onsubmit=function()	{
		document.getElementById("uploadTarget").onload = uploadDone(); //This function should be called when the iframe has compleated loading
		// That will happen when the file is completely uploaded and the server has returned the data we need.
	}
}

function uploadDone() { //Function will be called when iframe is loaded
	var ret = frames['uploadTarget'].document.getElementsByTagName("body")[0].innerHTML;
	var data = eval("("+ret+")"); //Parse JSON 
	str = data.file_name;
	  
	if(data.failure!='invalid') {
		if(str){ //on success
			$('#dPic').attr({src: str});
			$('.avatar').attr({src: data.avatar_icon});
			$('#dPic').attr({src: str});
			$('#dpOverlay').hide();
			// show success message
			alert("Your profile photo has been changed");
			
		}  else  { //on failure
			// show error message
			alert("Sorry could not upload your image");
			$('#dpOverlay').hide();
			                   
		}
	} else {
			alert("It seems you have uploaded a invalid/corrupted image file. please upload .jpg, .png, .gif file only"); 

	
	}
}